package com.example.demo.thread.loopPrint;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Lock_ABC {
    private Integer num = 0;
    private Lock lock = new ReentrantLock();

    private void printABC(int targetNum){
        for(int i=0;i<50;){
            lock.lock();
            if(num % 3 == targetNum){
                System.out.println(Thread.currentThread().getName() + ":" + num);
                num++;
                //重点，打印后再进行下一个循环
                i++;
            }
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        Lock_ABC lock_abc = new Lock_ABC();
        new Thread(() -> lock_abc.printABC(0), "A").start();
        new Thread(() -> lock_abc.printABC(1), "B").start();
        new Thread(() -> lock_abc.printABC(2), "C").start();
    }

}
